package com.googlecode.animator.data.objects3d
{
	import com.googlecode.animator.data.IValueObject;
	import com.googlecode.animator.data.materials.BitmapFileMaterialVO;
	import com.googlecode.animator.data.materials.ColorMaterialVO;
	import com.googlecode.animator.data.materials.FileAssetMaterialVO;
	import com.googlecode.animator.data.materials.MaterialVO;
	import com.googlecode.animator.data.materials.MovieMaterialVO;
	import com.googlecode.animator.data.materials.TextMaterialVO;
	import com.googlecode.animator.data.materials.VideoStreamMaterialVO;
	
	public class SingleMaterialObject3DVO extends Object3DVO
	{
		
		public var material:MaterialVO;
		
		override public function clone(src:* = null):IValueObject
		{
			if(src == null)
			{
				src = super.clone(new SingleMaterialObject3DVO());
			}else
			{
				src = super.clone(src);
			}
			
			src.material = this.material.clone() as MaterialVO;
			
			return src;
		}
		
		override public function toXML(src:XML = null):XML
		{
			if(src == null)
			{
				src = super.toXML(<object3D name={this.name} label={this.label}></object3D>);
				
			}else
			{
				src = super.toXML(src);
			}
			
			src.appendChild(<material>{this.material.toXML()}</material>);

			return src;
		}
		
		override public function fromXML(src:XML):void
		{
			super.fromXML(src);
			
			var newMat:MaterialVO;

			if(src.material.*.name() == ColorMaterialVO.TYPE)
			{
				newMat = new ColorMaterialVO();
				(newMat as ColorMaterialVO).fillAlpha = src.material.*.fillAlpha;
				(newMat as ColorMaterialVO).fillColor = src.material.*.fillColor;
				 
			}
			
			if(src.material.*.name() == MovieMaterialVO.TYPE)
			{
				newMat = new MovieMaterialVO();
				(newMat as MovieMaterialVO).showControls = src.material.*.showControls == "true" ? true : false;
			}
			
			if(src.material.*.name() == TextMaterialVO.TYPE)
			{
				newMat = new TextMaterialVO();
				(newMat as TextMaterialVO).text = src.material.*.text;
			}
				
			if(src.material.*.name() == BitmapFileMaterialVO.TYPE)
			{
				newMat = new BitmapFileMaterialVO();
			}
			
			if(src.material.*.name() == VideoStreamMaterialVO.TYPE)
			{
				newMat = new VideoStreamMaterialVO();

				(newMat as VideoStreamMaterialVO).width = src.material.*.width;
				(newMat as VideoStreamMaterialVO).height = src.material.*.height;
				(newMat as VideoStreamMaterialVO).showControls = src.material.*.showControls == "true" ? true : false;
			}
			
			if(newMat is FileAssetMaterialVO)
			{
				(newMat as FileAssetMaterialVO).filePath = src.material.*.filePath;
				(newMat as FileAssetMaterialVO).isRelative = src.material.*.filePath.@isRelative == "true" ? true : false;
			}
			
			this.material = newMat;
		}
	}
}